#include "alarmDialogV1.h"
#include "ui_alarmDialogV1.h"

#include "MemoGlobal.h"
#include "memoDetailInfoWidget.h"

AlarmDialogV1::AlarmDialogV1(QSqlDatabase _db,
                             QWidget *parent)
    :m_SqlDB(_db),
    QDialog(parent),
    ui(new Ui::AlarmDialogV1)
{
    ui->setupUi(this);

//-----------------------------------
    initUIs();
    updateAlarms();

    m_pTimer = new QTimer(this);
    connect(m_pTimer, SIGNAL(timeout()),
            this, SLOT(checkAlarm()));
    m_pTimer->start(1000);
}

AlarmDialogV1::~AlarmDialogV1()
{
    delete ui;
}

void AlarmDialogV1::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//----------------------------------------------------------------------
void AlarmDialogV1::updateAlarms()//query db again
{
    m_pAlarmRecoedModel->setTable("AlarmMemoList");
    m_pAlarmRecoedModel->setSort(1,Qt::AscendingOrder);
    m_pAlarmRecoedModel->select();

    m_pAlarmQueryModel->setQuery("SELECT AlarmID, AlarmDate FROM AlarmMemoList ORDER BY AlarmDate ASC",this->m_SqlDB);

    //copy data to alarm list
    m_AlarmList.clear();
    for(int i=0;i<m_pAlarmQueryModel->rowCount();i++){
        AlarmStruct _newAlarm;
        _newAlarm.strID = m_pAlarmQueryModel->record(i).value("AlarmID").toString();
        _newAlarm.dtAlarmDate = QDateTime::fromString(m_pAlarmQueryModel->record(i).value("AlarmDate").toString().left(16),Memo::strDateTimeConvertFormat);
        m_AlarmList.append(_newAlarm);
    }

    m_AlarmIndex = -1;

    ui->ui_ComboBoxUnhandleAlarms->clear();
    ui->ui_ComboBoxAllAlarms->clear();

    for(int i=0;i<m_AlarmList.count();i++){
        ui->ui_ComboBoxAllAlarms->addItem(m_AlarmList.at(i).dtAlarmDate.toString(Memo::strDateTimeConvertFormat),
                                          QVariant(m_AlarmList.at(i).strID));

    }

    checkAlarm();
    ui->ui_ComboBoxAllAlarms->setCurrentIndex(ui->ui_ComboBoxAllAlarms->count()+1);
    ui->ui_ComboBoxUnhandleAlarms->setCurrentIndex(ui->ui_ComboBoxAllAlarms->count()+1);
}

void AlarmDialogV1::initUIs()
{
    m_pAlarmRecoedModel = new QSqlTableModel(this, this->m_SqlDB);
    m_pAlarmQueryModel = new QSqlQueryModel;

    m_pMemoDetailInfoWidget =
            new MemoDetailInfoWidget(this->m_SqlDB,
                                     "",
                                     MemoDetailInfoWidget::EDITABLE,
                                     this);

    ui->ui_DialogLayout->insertWidget(1,m_pMemoDetailInfoWidget);

    connect(ui->ui_ComboBoxUnhandleAlarms,SIGNAL(activated(int)),
            this, SLOT(doComboBoxUnhandleAlarmsActived(int)));

    connect(ui->ui_ComboBoxAllAlarms,SIGNAL(activated(int)),
            this, SLOT(doComboBoxAllAlarmsActived(int)));

    connect(ui->ui_ComboBoxUnhandleAlarms,SIGNAL(activated(int)),
            ui->ui_ComboBoxAllAlarms, SLOT(setCurrentIndex(int)));

    connect(ui->ui_ComboBoxAllAlarms,SIGNAL(activated(int)),
            ui->ui_ComboBoxUnhandleAlarms, SLOT(setCurrentIndex(int)));

    connect(ui->ui_BtnModify, SIGNAL(clicked()),
            this, SLOT(doBtnModifyMemo()));

    connect(ui->ui_BtnDeleteAlarm, SIGNAL(clicked()),
            this, SLOT(doBtnDeleteAlarm()));

    connect(ui->ui_BtnDeleteMemo, SIGNAL(clicked()),
            this, SLOT(doBtnDeleteMemo()));
}

void AlarmDialogV1::checkAlarm()
{
    QDateTime _dtCurrent = QDateTime::currentDateTime();

//    int _i = m_AlarmIndex+1;
    while(m_AlarmIndex<m_AlarmList.count()-1){
        if(m_AlarmList.at(m_AlarmIndex+1).dtAlarmDate<=_dtCurrent){
            ui->ui_ComboBoxUnhandleAlarms->addItem(m_AlarmList.at(m_AlarmIndex+1).dtAlarmDate.toString(Memo::strDateTimeConvertFormat),
                                                   QVariant(m_AlarmList.at(m_AlarmIndex+1).strID));
            m_AlarmIndex++;
        }else{
            break;
        }

    }

    emit ThereAreNewAlarms(m_AlarmIndex+1);//send signal
//    if(m_AlarmIndex>=0){
//        emit ThereAreNewAlarms(m_AlarmIndex+1);
//    }

//    if(m_AlarmIndex>=0){
//        this->m_pMemoDetailInfoWidget->fillDate(m_AlarmList.at(0).strID);
//    }


}

void AlarmDialogV1::closeEvent (QCloseEvent* _e)
{
//    qDebug()<<"AlarmDialogV1::closeEvent";
    if(this->isVisible()){
//        qDebug()<<this->isVisible();
        emit WeCannotSeeIt(false);

    }else{
//        qDebug()<<this->isVisible();
        emit WeCannotSeeIt(true);
    }
}

void AlarmDialogV1::keyPressEvent (QKeyEvent* _e)
{
    if(_e->key() == Qt::Key_Escape){
        emit WeCannotSeeIt(false);
    }

    QDialog::keyPressEvent(_e);
}

void AlarmDialogV1::updateComboxMemoLabel()
{
    m_pMemoDetailInfoWidget->updateComboxMemoLabel();
}

void AlarmDialogV1::doComboBoxUnhandleAlarmsActived(int i)
{
    QString _strAlarmID = ui->ui_ComboBoxUnhandleAlarms->itemData(i).toString();
    this->m_pMemoDetailInfoWidget->fillDate(_strAlarmID);
}

void AlarmDialogV1::doComboBoxAllAlarmsActived(int i)
{
    QString _strAlarmID = ui->ui_ComboBoxAllAlarms->itemData(i).toString();
    this->m_pMemoDetailInfoWidget->fillDate(_strAlarmID);
}

void AlarmDialogV1::doBtnDeleteAlarm()
{
    if(this->m_pMemoDetailInfoWidget->deleteAlarm()){
        this->updateAlarms();
        m_pMemoDetailInfoWidget->fillDate("");
    }
}

void AlarmDialogV1::doBtnDeleteMemo()
{
    if(this->m_pMemoDetailInfoWidget->deleteRecord()){
        this->updateAlarms();
        m_pMemoDetailInfoWidget->fillDate("");
    }
}

void AlarmDialogV1::doBtnModifyMemo()
{
    if(this->m_pMemoDetailInfoWidget->updateRecord()){
        this->updateAlarms();
        int index = ui->ui_ComboBoxAllAlarms->findData(QVariant(m_pMemoDetailInfoWidget->getID()));
        if(index<0){
            m_pMemoDetailInfoWidget->fillDate("");
        }else{
            ui->ui_ComboBoxAllAlarms->setCurrentIndex(index);
            ui->ui_ComboBoxUnhandleAlarms->setCurrentIndex(index);
        }

    }
}


